package com.tanhua.autoconfig.templates;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.properties.AipFaceProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-08 15:10
 **/
public class AipFaceTemplate {

    //设置APPID/AK/SK
   @Autowired
   private AipFaceProperties aipFaceProperties;


    /*
    * 测试是否包含人脸
    *       参数：图片url
    *       返回值：
    *            true 包含，false 不包含
    *
    **/
    public Boolean detect(String image){
        // 初始化一个AipFace
        AipFace client = new AipFace(aipFaceProperties.getAppId(),
                                     aipFaceProperties.getApiKey(),
                                     aipFaceProperties.getSecretKey());

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        //图片(url,base64编码,图片token字符串)
        //String image = "https://tanhua-ph1.oss-cn-beijing.aliyuncs.com/2022/03/08/8779a03e-de09-411b-a782-c224b5e95f93.png";
        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));
        int error_code = res.getInt("error_code");
        return error_code == 0;
    }
}
